<?php

// phpMyRealty 3
//
// File Name: config.php
// File Location : ./admin/
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');
define('PMRADMIN', 'true');

include ( '.././config.php' );
include ( PATH . '/defaults.php' );

// ----------------------------------------------------------------------
// ADMIN PANEL / EDIT CONFIGURATION SETTINGS

// Title tag value
$title = $lang['Admin_Configuration_Settings'];

// Template header
include ( PATH . '/admin/template/header.php' );

// If logged we can start the page output
if (adminAuth($session->fetch('adminlogin'), $session->fetch('adminpassword')))

 {
  // Include navigation panel
  include ( PATH . '/admin/navigation.php' );
  // Make sure this administrator have access to this script
  adminPermissionsCheck('manage_settings', $session->fetch('adminlogin')) or error ('Critical Error', 'Incorrect privileges');

  // If the form is submitted we save the data
  if (isset($_POST['submit']) && $_POST['submit'] == $lang['Admin_Settings_Submit'])
   {

    echo table_header ( $lang['Information'] );

    $form = array_map ( 'safehtml', $_POST );

    // Initially we think that no errors were found
    $count_error = 0;
    
	// Validate language before setting it
	switch ($form['language']) {
    case 'svenska':
        $form['language'] = 'swedish';
        break;
	}
	if (!file_exists(PATH . '/languages/' . $form['language'] . '.lng.php'))
	{
		$error_msg[] = 'The default language you specified does not exist. All available languages can be viewed in the \'languages/\' directory.';
		$count_error++;
	}
	elseif (empty($installed_languages[$form['language']]))
    {
        $error_msg[] = 'The default language you specified is not enabled. All available languages can be set in the \'languages/settings.php\' file.';
        $count_error++;
    }
	
	// Validate template before setting it
	if (!is_dir(PATH . '/templates/' . $form['template']))
	{
		$error_msg[] = 'The default template you specified does not exist. All available templates can be viewed in the \'templates/\' directory.';
		$count_error++;
	}
	
	if ($count_error == 0)
     {
      // Update the configuration records in the database

      $sql = 'SELECT * FROM ' . CONFIGURATION_TABLE;
      $r = $db->query( $sql );
	  $error = 0;
	
      while ($f = $db->fetcharray($r))
       {

        $name = $f['name'];
		
		$sql = 'UPDATE ' . CONFIGURATION_TABLE . ' SET val = "' . $form[$name] . '" WHERE name = "' . $name . '" LIMIT 1';
		$db->query( $sql ) or error ('Critical Error', mysql_error () );

       }

      echo $lang['Admin_Settings_Updated'];

     }
     	else
     {
     
     	// Output the errors
     	echo '<span class="warning">' . $count_error . ' ' . $lang['Errors_Found'] . ':<br><br>';
     	foreach ($error_msg AS $key => $value)
     	{
     		echo $value . '<br>';
     	}
     	echo '</span>';
     
     }

    echo table_footer ( );

   }

  // Fetching the configuration data from the database
  $sql = 'SELECT * FROM ' . CONFIGURATION_TABLE;
  $r = $db->query( $sql );

  // Generating the configuration form
  echo table_header ( $lang['Admin_Configuration_Settings'] );

  echo '<form action = "' . URL . '/admin/config.php" method="POST">';
  echo ' <table cellpadding="5" cellspacing="0" border="0">';

  while ($f = $db->fetcharray( $r ))

   {
 
    echo configform ($f['name'], $f['val'], $f['descr']);

   }

  echo ' <tr><td colspan="3"><input type="Submit" name="submit" value="' . $lang['Admin_Settings_Submit'] . '"></td></tr>';

  echo ' </table>
       </form>'; 

  echo table_footer ();

 }

else

 {

  error ('Critical Error' , 'Please, login to access this script.');

 }

// Template footer
include ( PATH . '/admin/template/footer.php' );

?>